package meijuli.com.join51.business.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import meijuli.com.join51.business.R;
import meijuli.com.join51.business.model.PayPrice;

import java.util.List;

/**
 * 预购金额适配器
 *
 * @author wenqin 2017-07-13 09:42
 */

public class PayPriceAdapter extends RecyclerView.Adapter<PayPriceAdapter.Vh> {

    private Context mContext;
    private List<PayPrice> mPayPrices;
    private LayoutInflater mInflater;
    private OnItemClickListener mOnItemClickListener;
    private int mSelectedPosition = 0;

    public PayPriceAdapter(Context context, List<PayPrice> payPrices) {
        mContext = context;
        mPayPrices = payPrices;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public Vh onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Vh(mInflater.inflate(R.layout.adapter_recharge_money, parent, false));
    }

    @Override
    public void onBindViewHolder(Vh holder, int position) {
        if (mSelectedPosition == position) {
            holder.mRechargeText.setBackgroundResource(R.drawable.btn_login_selector);
            holder.mRechargeText.setTextColor(ContextCompat.getColor(mContext, R.color.white));
        } else {
            holder.mRechargeText.setBackgroundResource(R.drawable.shape_black_stroke_cor_6);
            holder.mRechargeText.setTextColor(ContextCompat.getColor(mContext, R.color.black_normal));
        }
        holder.mRechargeText.setText(mPayPrices.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return mPayPrices == null ? 0 : mPayPrices.size();
    }

    class Vh extends RecyclerView.ViewHolder {
        TextView mRechargeText;

        public Vh(View itemView) {
            super(itemView);
            mRechargeText = itemView.findViewById(R.id.recharge_text);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mSelectedPosition >= 0) {
                        notifyItemChanged(mSelectedPosition);
                    }
                    mSelectedPosition = getAdapterPosition();
                    notifyItemChanged(mSelectedPosition);
                    if (mOnItemClickListener != null && getAdapterPosition() >= 0) {
                        mOnItemClickListener.payPriceClick(getAdapterPosition());
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        void payPriceClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public PayPrice getSelectedPayPrice() {
        return mPayPrices.get(mSelectedPosition);
    }
}
